#include "Spring.h"
#include "Particle.h"
#include <glut.h>
#include <assert.h>

void Spring::Update()
{
	assert(m_Particles[0]);
	assert(m_Particles[1]);

	//get distance
	Vec3f diff = m_Particles[0]->ReturnPos() - m_Particles[1]->ReturnPos();

	//get distance between dist and natural length
	float x = diff.Length() - m_Natural;

	//hookes law says: F = -K X
	Vec3f force = Normalised(diff) * -m_K * x;

	//apply force to particle 1;
	m_Particles[0]->ApplyForce(force);

	//apply force to particle 2;
	m_Particles[1]->ApplyForce(-force);

}

void Spring::Draw()
{
	glPushMatrix();

	glBegin(GL_LINES);
		glVertex3f(m_Particles[0]->ReturnPos().m_x, m_Particles[0]->ReturnPos().m_y, m_Particles[0]->ReturnPos().m_z);
		glVertex3f(m_Particles[1]->ReturnPos().m_x, m_Particles[1]->ReturnPos().m_y, m_Particles[1]->ReturnPos().m_z);
	glEnd();

}

void Spring::Init(float k, float MinLength, float MaxLength, float NaturalLength)
{
	m_K = k;
	m_Min = MinLength;
	m_Max = MaxLength;
	m_Natural = NaturalLength;
}


void Spring::SetParticles(Particle& p1, Particle& p2)
{
	m_Particles[0] = &p1;
	m_Particles[1] = &p2;

}